<html>
<head>
<title>Code Fragment</title>
</head>

<body text=#000000 bgcolor=#ffffff>
<center>
</center><br><br><dl><dd><pre>

<font color=#ff8000>import</font> java.awt.*;
<font color=#ff8000>import</font> java.awt.event.*;
<font color=#ff8000>import</font> jdsl.core.ref.ComparableComparator;


<font color=#ff0080>/**
  * This simple phone number database supports insertion and removal of
  * names and corresponding phone numbers, and allows the user to retrieve
  * a phone number by entering a name. The program demonstrates some
  * capabilities of the JDSL's Dictionary and OrderedDictionary
  * interfaces.
	*
	* @author Lucy Perry (lep)
	* @version JDSL 2
*/</font>


<font color=#8000a0>public</font> <font color=#8000a0><font color=#ff8000>class</font> </font>PhoneBook <font color=#8000a0><font color=#ff8000>extends</font> </font>Frame
  <font color=#8000a0><font color=#ff8000>implements</font> </font>ActionListener, WindowListener{
    
    <font color = #ff0080>/* ************************************ */</font> 
    <font color = #ff0080>/* The members described in the lesson. */</font>
    <font color = #ff0080>/* ************************************ */</font> 

    <font color=#ff0080>//constructor</font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#0000ff>PhoneBook</font>(){
      <font color=#ff8000>super</font><font color=#0000ff></font>();

      <font color=#ff0080>//b5.1</font>
      <font color=#ff0080>//create the OrderedDictionary, passing a Comparator to its constructor</font>
      od_ = <font color=#8000a0><font color=#ff8000>new</font> </font>jdsl.core.ref.<font color=#0000ff>RedBlackTree</font>(<font color=#ff8000>new</font> <font color=#0000ff>ComparableComparator</font>());
      <font color=#ff0080>//e5.1</font>
      <font color=#0000ff>setupPanel</font>();<font color=#ff0080>//takes care of visual components</font>
    }

    <font color=#ff0080>/**
     * This method is called when the "add a name,number entry" button is
     * clicked. The String in the name_adder_ field is used as the key, and
     * the String (phone number) in the number_adder_ field is used as the
     * element to store.
     * The key and element are then inserted into the OrderedDictionary.
     */</font>
    <font color=#ff0080>//b5.2</font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>addEntry</font>(){
      <font color=#8000a0>Object </font>key = name_adder_.<font color=#0000ff>getText</font>();
      <font color=#8000a0>Object </font>element = number_adder_.<font color=#0000ff>getText</font>();
      od_.<font color=#0000ff>insert</font>(key, element);
      <font color=#ff0080>//now clear out those text fields just to look nice</font>
      name_adder_.<font color=#0000ff>setText</font>(<font color=#008000>""</font>);
      number_adder_.<font color=#0000ff>setText</font>(<font color=#008000>""</font>);
    }
    <font color=#ff0080>//e5.2</font>

    <font color=#ff0080>/*
     * This method is called when the "find the phone number" button is
     * clicked. The String in the name_query_ field is used as a search key.
     * That key is passed as a parameter to the Dictionary's find(.) method.
     * A Locator is returned from the find(.) operation, and is stored as our
     * current_entry_. Then the displayCurrentEntry() helper method is
     * invoked.
     */</font>
    <font color=#ff0080>//b5.3 </font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>findEntry</font>(){
      <font color=#8000a0>Object </font>key = name_query_.<font color=#0000ff>getText</font>();
      <font color=#ff0080>// note that a JDSL Locator is returned from the find(.) operation</font>
      current_entry_ = od_.<font color=#0000ff>find</font>(key);

      <font color=#ff0080>//this helper method will display the data</font>
      <font color=#0000ff>displayCurrentEntry</font>();

      <font color=#ff0080>//now clear out the name_query_ text field just to look nice</font>
      name_query_.<font color=#0000ff>setText</font>(<font color=#008000>""</font>);
    }
    <font color=#ff0080>//e5.3</font>

    <font color=#ff0080>/**
     * This method is called when the "previous entry" button is 
     * clicked. It uses the OrderedDictionary method before(Locator).
     */</font>
    <font color=#ff0080>//b5.4</font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>getEntryBefore</font>(){
      <font color=#ff0080>//we want to see the entry before the current_entry_</font>
      <font color=#ff0080>//note that the before(.) method takes a Locator as a parameter</font>
      <font color=#ff8000>try</font>{
	    jdsl.core.api.Locator prevEntry = od_.<font color=#0000ff>before</font>(current_entry_);
	    current_entry_ = prevEntry;
      }
      <font color=#ff8000>catch</font><font color=#0000ff></font>(jdsl.core.api.InvalidAccessorException iae){}
      <font color=#ff0080>//this would happen if we tried calling before(.) using an invalid</font>
      <font color=#ff0080>//Locator for current_entry_.</font>

      <font color=#0000ff>displayCurrentEntry</font>();<font color=#ff0080>//Display the current (valid or not) entry.</font>
    }
    <font color=#ff0080>//e5.4</font>

    <font color=#ff0080>/**
     * This method is called when the "next entry" button is clicked.
     * It uses the OrderedDictionary method after(Locator).
     */</font>
    <font color=#ff0080>//b5.5 </font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>getEntryAfter</font>(){
      <font color=#ff0080>//we want to see the entry after the the current_entry_</font>
      <font color=#ff0080>//note that the after(.) method takes a Locator as a parameter</font>
      <font color=#ff8000>try</font>{
	    jdsl.core.api.Locator nextEntry = od_.<font color=#0000ff>after</font>(current_entry_);
	    current_entry_ = nextEntry;
      }
      <font color=#ff8000>catch</font><font color=#0000ff></font>(jdsl.core.api.InvalidAccessorException iae){}
      <font color=#ff0080>//this would happen if we tried calling after(.) using an invalid</font>
      <font color=#ff0080>//Locator for current_entry_.</font>

      <font color=#0000ff>displayCurrentEntry</font>();<font color=#ff0080>//Display the current (valid or not) entry.</font>
    }
    <font color=#ff0080>//e5.5</font>
    
    <font color = #ff0080>/* ************************************ */</font> 
    <font color = #ff0080>/* Members not described in the lesson. */</font>
    <font color = #ff0080>/* ************************************ */</font> 
    
   <font color=#ff0080>//JDSL OrderedDictionary for keeping track of data</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>jdsl.core.api.OrderedDictionary od_;
    
    <font color=#ff0080>//This JDSL Locator contains the name,number pair that is displayed</font>
    <font color=#ff0080>//after a find(.) operation</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>jdsl.core.api.Locator current_entry_;

    <font color=#ff0080>//awt panel, holds all visual components</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>Panel panel_;

    <font color=#ff0080>//user enters a person's name here</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>TextField name_adder_;

    <font color=#ff0080>//user enters a person's phone number here</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>TextField number_adder_;

    <font color=#ff0080>//click this button to enter a name+number in the OrderedDictionary</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>Button add_entry_;

    <font color=#ff0080>//user enters a name here for retrieving a phone number</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>TextField name_query_;

    <font color=#ff0080>//user clicks this button to retrieve a phone number</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>Button find_entry_;

    <font color=#ff0080>//data resulting from a "find" operation is displayed here</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>TextField show_entry_;
    
    <font color=#ff0080>//click this button for the previous entry in the phone book</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>Button prev_entry_;

    <font color=#ff0080>//click this button for the next entry in the phone book</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>Button next_entry_;

    <font color=#ff0080>//click this button to quit</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font>Button quit_;

    <font color=#ff0080>/*
      This method displays the element and key of the Locator which is
      the current_entry_. It also handles the display of two types of 
      invalid Locators which may result from a find(.), before(.), or 
      after(.) operation.
    */</font>
    <font color=#ff0080>//b5.6</font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>displayCurrentEntry</font>(){
      <font color=#8000a0><font color=#8000a0>String</font> </font>toDisplay = null;

      <font color=#ff0080>//if a find(.) operation was unsuccessful, it returned NO_SUCH_KEY</font>
      <font color=#ff8000>if</font><font color=#0000ff> </font>(current_entry_ == jdsl.core.api.Dictionary.NO_SUCH_KEY){
	toDisplay = <font color=#008000>"No entry was found using that name as a key."</font>;
      }

      <font color=#ff0080>//if before(.) or after(.) was requested, and there was no such entry</font>
      <font color=#8000a0><font color=#ff8000>else</font> </font><font color=#ff8000>if</font><font color=#0000ff> </font>(current_entry_ == jdsl.core.api.OrderedDictionary.BOUNDARY_VIOLATION){
	toDisplay = <font color=#008000>"You've passed the beginning or end of the Dictionary."</font>;
      }
      
      <font color=#ff8000>else</font>{
	toDisplay =<font color=#0000ff> </font>(<font color=#8000a0>String</font>)<font color=#0000ff></font>(current_entry_.<font color=#0000ff>key</font>()) + 
	  <font color=#008000>" "</font> +<font color=#0000ff> </font>(<font color=#8000a0>String</font>)<font color=#0000ff></font>(current_entry_.<font color=#0000ff>element</font>());
      }	
      
      <font color=#ff0080>//put the string in the text field</font>
      show_entry_.<font color=#0000ff>setText</font>(toDisplay);

    }
    <font color=#ff0080>//e5.6</font>

    <font color=#ff0080>//takes care of visual components</font>
    <font color=#8000a0><font color=#8000a0>private</font> </font><font color=#8000a0>void</font> <font color=#0000ff>setupPanel</font>(){
      <font color=#0000ff>setLocation</font>(50,50);<font color=#ff0080>//makes frame pop up in nice location</font>

      panel_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>Panel</font>();
      <font color=#0000ff>add</font>(panel_);

      name_adder_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>TextField</font>(25);
      name_adder_.<font color=#0000ff>setEditable</font>(true);

      number_adder_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>TextField</font>(25);
      number_adder_.<font color=#0000ff>setEditable</font>(true);

      add_entry_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>Button</font>(<font color=#008000>"Click to add a name,number entry to the phone book"</font>);
      add_entry_.<font color=#0000ff>addActionListener</font>(<font color=#ff8000>this</font>);

      name_query_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>TextField</font>(25);
      name_query_.<font color=#0000ff>setEditable</font>(true);

      find_entry_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>Button</font>(<font color=#008000>"Click to find the phone number for the given name"</font>);
      find_entry_.<font color=#0000ff>addActionListener</font>(<font color=#ff8000>this</font>);

      show_entry_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>TextField</font>(45);
      show_entry_.<font color=#0000ff>setEditable</font>(false);

      prev_entry_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>Button</font>(<font color=#008000>"Click for the previous entry in phone book"</font>);
      prev_entry_.<font color=#0000ff>addActionListener</font>(<font color=#ff8000>this</font>);

      next_entry_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>Button</font>(<font color=#008000>"Click for the next entry in phone book"</font>);
      next_entry_.<font color=#0000ff>addActionListener</font>(<font color=#ff8000>this</font>);
      
      quit_ = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>Button</font>(<font color=#008000>"Quit"</font>);
      quit_.<font color=#0000ff>addActionListener</font>(<font color=#ff8000>this</font>);

      <font color=#0000ff>addWindowListener</font>(<font color=#ff8000>this</font>);

      <font color=#8000a0>GridBagLayout </font>layout = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>GridBagLayout</font>();
      panel_.<font color=#0000ff>setLayout</font>(layout);

      <font color=#8000a0>GridBagConstraints </font>constraints = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>GridBagConstraints</font>();
      constraints.gridy=GridBagConstraints.RELATIVE;<font color=#ff0080>//right below the last one</font>
      constraints.gridx=1;<font color=#ff0080>//1 column</font>
      constraints.anchor=GridBagConstraints.CENTER;
      constraints.ipady=2;<font color=#ff0080>//set vgap to 2 pixels.</font>
      constraints.ipadx=3;


      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>"Enter a name here:"</font>), constraints);
      panel_.<font color=#0000ff>add</font>(name_adder_, constraints);

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>"Enter a phone number here:"</font>), constraints);
      panel_.<font color=#0000ff>add</font>(number_adder_, constraints);

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>
      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>

      panel_.<font color=#0000ff>add</font>(add_entry_, constraints);

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>"Find phone number for this person (enter name):"</font>),
		 constraints);
      panel_.<font color=#0000ff>add</font>(name_query_, constraints);
      panel_.<font color=#0000ff>add</font>(find_entry_, constraints);

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>
      
      panel_.<font color=#0000ff>add</font>(show_entry_, constraints);
      panel_.<font color=#0000ff>add</font>(prev_entry_, constraints);
      panel_.<font color=#0000ff>add</font>(next_entry_, constraints);

      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>
      panel_.<font color=#0000ff>add</font>(<font color=#ff8000>new</font> <font color=#0000ff>Label</font>(<font color=#008000>""</font>), constraints);<font color=#ff0080>//blank line</font>

      panel_.<font color=#0000ff>add</font>(quit_, constraints);

      <font color=#0000ff>validate</font>();
      <font color=#0000ff>setVisible</font>(true);
      panel_.<font color=#0000ff>validate</font>();
      panel_.<font color=#0000ff>setVisible</font>(true);
      <font color=#0000ff>pack</font>();
    }


    <font color=#ff0080>//handles button presses</font>
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>actionPerformed</font>(ActionEvent e){
      
      <font color=#ff8000>if</font><font color=#0000ff> </font>(e.<font color=#0000ff>getSource</font>() == add_entry_){
	    <font color=#0000ff>addEntry</font>();
      }
      <font color=#8000a0><font color=#ff8000>else</font> </font><font color=#ff8000>if</font><font color=#0000ff> </font>(e.<font color=#0000ff>getSource</font>() == find_entry_){
	    <font color=#0000ff>findEntry</font>();
      }
      <font color=#8000a0><font color=#ff8000>else</font> </font><font color=#ff8000>if</font><font color=#0000ff> </font>(e.<font color=#0000ff>getSource</font>() == prev_entry_){
	    <font color=#0000ff>getEntryBefore</font>();
      }
      <font color=#8000a0><font color=#ff8000>else</font> </font><font color=#ff8000>if</font><font color=#0000ff> </font>(e.<font color=#0000ff>getSource</font>() == next_entry_){
	    <font color=#0000ff>getEntryAfter</font>();
      }
      <font color=#8000a0><font color=#ff8000>else</font> </font><font color=#ff8000>if</font><font color=#0000ff> </font>(e.<font color=#0000ff>getSource</font>() == quit_){
	    System.<font color=#0000ff>exit</font>(0);
      }

    }


    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>static</font> <font color=#8000a0><font color=#8000a0>void</font> </font><font color=#0000ff>main</font>(<font color=#8000a0>String</font> args[]){
      <font color=#8000a0>PhoneBook </font>pb = <font color=#8000a0><font color=#ff8000>new</font> </font><font color=#0000ff>PhoneBook</font>();
    }

    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowActivated</font>(WindowEvent e) {}
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowClosed</font>(WindowEvent e) {}
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowClosing</font>(WindowEvent e) {System.<font color=#0000ff>exit</font>(0);}
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowDeactivated</font>(WindowEvent e) {}
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowDeiconified</font>(WindowEvent e) {}
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowIconified</font>(WindowEvent e) {}
    <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>windowOpened</font>(WindowEvent e) {}
}

      
      
</dl>
</body>
</html>
